<?php

namespace App\Http\Controllers\Admin\Dispatch;

use App\Http\Controllers\Controller;
use App\Http\Requests\dispatch\DriverRequest;
use App\Models\dispatch\CarModel;
use App\Models\dispatch\DriverModel;
use Illuminate\Http\Request;

class Driver extends Controller
{
    use DriverRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $name = $request->input('name', '');
        $type = $request->input('type', '');
        $create_start = $request->input('create_start', '');
        $create_end = $request->input('create_end', '');

        $query = DriverModel::query()->with('create_admin');

        if($name){
            $query = $query->where('name', 'like', '%'.$name.'%');
        }

        if($type){
            $query = $query->where('type', $type);
        }

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'type' => $item->type,
                'type_name' => DriverModel::TYPE[$item->type],
                'name' => $item->name,
                'mobile' => $item->mobile,
                'hk_mobile' => $item->hk_mobile,
                'login_mobile' => $item->login_mobile,
                'id_number' => $item->id_number,
                'card_number' => $item->card_number,
                'created_at' => $item->created_at->toDateTimeString(),
                'create_user' => $item->create_admin ? $item->create_admin->staff->name: '',
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    /**
     * Notes:添加
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $find = DriverModel::where('name', $params['name'])->first();
        if($find){
            return $this->error('司机已存在');
        }

        $find = DriverModel::where('login_mobile', $params['login_mobile'])->first();
        if($find){
            return $this->error('登录手机号已存在');
        }

        $model = new DriverModel();
        $model->type = $params['type'];
        $model->name = $params['name'];
        $model->mobile = $params['mobile'];
        $model->hk_mobile = $params['hk_mobile'] ?? '';
        $model->id_number = $params['id_number'];
        $model->card_number = $params['card_number'] ?? '';
        $model->login_mobile = $params['login_mobile'];
//        $model->login_password = $params['login_password'];
        $model->created_admin_id = $auth->id;
        $model->save();

        return $this->success();
    }

    public function update(Request $request, $id){
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $model = DriverModel::find($id);
        if (!$model) {
            return $this->error('数据不存在');
        }

        if($model->name != $params['name']){
            $find = DriverModel::where('name', $params['name'])->first();
            if($find){
                return $this->error('司机已存在');
            }
        }

        if($model->login_mobile != $params['login_mobile']){
            $find = DriverModel::where('login_mobile', $params['login_mobile'])->first();
            if($find){
                return $this->error('登录手机号已存在');
            }
        }

        $model->type = $params['type'];
        $model->name = $params['name'];
        $model->mobile = $params['mobile'];
        $model->hk_mobile = $params['hk_mobile'] ?? '';
        $model->id_number = $params['id_number'];
        $model->card_number = $params['card_number'] ?? '';
        $model->login_mobile = $params['login_mobile'];
//        $model->login_password = $params['login_password'];
        $model->save();

        return $this->success();
    }


    public function delete(int $id){
        $model = DriverModel::find($id);
        if(!$model) {
            return $this->error('记录不存在');
        }
        $model->delete();
        return $this->success();
    }

    public function select(Request $request)
    {
        $car_id = $request->input('id', '');

        $driver_id = CarModel::where('id', $car_id)->value('driver_ids');
        $driver_id = $driver_id ? json_decode($driver_id, true) : [];

        $list = DriverModel::whereIn('id', $driver_id)->select('id', 'name', 'mobile', 'id_number')->get()->toArray();
        return $this->success($list);
    }

}
